﻿

using GoodDotNet.Core.Data;
using GoodDotNet.Core.Entity;
using GoodDotNet.Core.IRepository;
using GoodDotNet.Core.Model;
using GoodDotNet.Core.Repository.BASE;

namespace GoodDotNet.Core.Repository
{
   public class  AdminModuleRepository :  BaseRepository< AdminModule>, AdminModuleIRepository
    {
        public  AdminModuleRepository(MyDbContext context) : base(context)
        {
        }

        public List<RouteModel> List()
        {
            var modules = context.AdminModule.Where(a => a.Deleted == 0).OrderByDescending(a => a.Sn).OrderBy(a => a.Sn).ToArray();
            return (ConvertRoute(modules));
        }
        /// <summary>
        /// 根据角色id查询权限
        /// </summary>
        /// <param name="role">角色id</param>
        /// <returns></returns>
        public List<RouteModel> ListByRole(int role)
        {
            var query = from a in context.AdminRoleModule
                        join b in context.AdminModule
                        on a.ModuleId equals b.Id
                        where a.RoleId == role
                        select b;
            var modules =query.OrderBy(a => a.Sn).ToArray();
            return (ConvertRoute(modules));     
        }
        /// <summary>
        /// 查询模块的角色权限
        /// </summary>
        /// <param name="ModuleId">模块id</param>
        /// <param name="site">站点</param>
        /// <returns></returns>
        public List<AdminRoleModuleGetListByModule> GetListByModule(int ModuleId,int site)
        {
            var query = from r in context.AdminRole
                        join b in context.AdminRoleModule
                        on r.Id equals b.RoleId
                        //into c
                        //from d in c.DefaultIfEmpty()
                        where r.Deleted == 0 &&r.Site==site 
                        select new AdminRoleModuleGetListByModule
                        {

                          RoleName=   r.RoleName,
                          Id=  r.Id,
                          Description=  r.Description,
                          Btns=  b.Btns,
                          hasPermission=(b.ModuleId==ModuleId)
                        };
           
            return query.Distinct().ToList();
        }
        /// <summary>
        /// 多个角色的权限
        /// </summary>
        /// <param name="roles"></param>
        /// <returns></returns>
        public List<RouteModel> ListByRoles(int[] roles)
        {
            var query = from a in context.AdminRoleModule
                        join b in context.AdminModule
                        on a.ModuleId equals b.Id
                        where roles.Contains(a.RoleId) && b.Deleted==0
                        select
                        new AdminModule
                        {
                            ParentId= b.ParentId,
                            Sn=b.Sn,
                            Title=b.Title,
                            Hidden=b.Hidden,
                            Path=b.Path,
                            Component=b.Component,
                            Icon=b.Icon,
                            Name=b.Name,
                            Id=b.Id,
                            Redirect=b.Redirect,
                            Btns= a.Btns
                        };
            var modules = query.GroupBy(a=>a.Id).Select(g=>g.First()).ToArray();
            
            return (ConvertRoute(modules.OrderBy(a=>a.Sn).ToArray()));
        }
        /// <summary>
        /// 转换成rote
        /// </summary>
        /// <param name="modules"></param>
        /// <returns></returns>
        private List<RouteModel> ConvertRoute(AdminModule[] modules)
        {
            List<RouteModel> routes = new List<RouteModel>();
            for (int i = 0; i < modules!.Count(); i++)
            {
                if (modules![i].ParentId == 0)
                {
                    RouteModel module = new RouteModel();

                    module.title = modules[i].Title;
                    module.path = modules[i].Path;
                    module.redirect = modules[i].Redirect;
                    module.component = modules[i].Component;
                    module.icon = modules[i].Icon;
                    module.name = modules[i].Name;
                    module.id=modules[i].Id;
                    module.btns = modules[i].Btns;
                    module.meta = new Meta
                    {
                        btns = modules[i].Btns,
                        title = modules[i].Title,

                    };
                        
                    
                    
                    module.children = Children(modules!, modules[i].Id);
                    routes.Add(module);
                }
            }
            return routes;
        }
        /// <summary>
        /// 遍历子模块
        /// </summary>
        /// <param name="modules">模块数组</param>
        /// <param name="parent">父id</param>
        private List<RouteModel> Children(AdminModule[] modules, int parent)
        {
            List<RouteModel> routes = new List<RouteModel>();
            for (int i = 0; i < modules!.Count(); i++)
            {
                if (modules[i].ParentId == parent)
                {
                    RouteModel module = new RouteModel();

                    module.title = modules[i].Title;
                    module.path = modules[i].Path;
                    module.redirect = modules[i].Redirect;
                    module.component = modules[i].Component;
                    module.icon = modules[i].Icon;
                    module.name = modules[i].Name;
                    module.id = modules[i].Id;
                    module.btns = modules[i].Btns;
                    module.hidden = modules[i].Hidden;
                    module.meta = new Meta
                    {
                        btns = modules[i].Btns,
                        title = modules[i].Title,
                        hidden = modules[i].Hidden

                    };

                    module.children = Children(modules, modules[i].Id);

                    routes.Add(module);
                }
            }
            return routes;
        }
       
    }
 }
